package com.gavin.plugin.database.help;

import java.sql.SQLException;
import java.util.HashSet;

import com.gavin.model.DataType;
import com.gavin.model.JObject;
import com.gavin.model.Model;
import com.gavin.model.Request;
import com.gavin.model.Result;
import com.gavin.plugin.database.ext.MathExt;
import com.gavin.schema.dataservice.Field;
import com.gavin.schema.dataservice.HandleTrans;
import com.gavin.schema.dataservice.HandleTransItem;
import com.gavin.schema.dataservice.SetVar;
import com.gavin.schema.dataservice.Value;
import com.gavin.schema.dataservice.type.SetVarTypeType;
import com.gavin.schema.dataservice.type.ValueTypeType;
import com.gavin.utils.Utility;

public class ValueUtils {
	/**
	 * 根据FieldIdText得到值
	 * 
	 * @param strFieldIdText
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public static  boolean getBooleanValue(String strFieldIdText, Model model) {
		// 解析strFieldIdText,判断出是否为FieldId
		StringBuilder strbFieldIdText = new StringBuilder();
		boolean bIsFieldId = handleFieldIdText(strFieldIdText, strbFieldIdText);

		if (bIsFieldId == false) {
			return Boolean.parseBoolean(strbFieldIdText.toString());
		} else {
			JObject obj=model.getJObject(strbFieldIdText.toString());
			if(obj==null){
				throw new RuntimeException("boolean value invalid error:"+strFieldIdText+"\n"+model);
			}
			return Boolean.valueOf(obj.getValue()+"");
		}
	}

	/**
	 * 根据FieldIdText得到值
	 * 
	 * @param strFieldIdText
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public static  byte getByteValue(String strFieldIdText, Model model) {
		// 解析strFieldIdText,判断出是否为FieldId
		StringBuilder strbFieldIdText = new StringBuilder();
		boolean bIsFieldId = handleFieldIdText(strFieldIdText, strbFieldIdText);

		if (bIsFieldId == false) {
			return Byte.parseByte(strbFieldIdText.toString());
		} else {
			JObject obj=model.getJObject(strbFieldIdText.toString());
			if(obj==null){
				throw new RuntimeException("byte value invalid error:"+strFieldIdText+"\n"+model);
			}
			return Byte.valueOf(obj.getValue()+"");
		}
	}

	/**
	 * 根据FieldIdText得到值
	 * 
	 * @param strFieldIdText
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public static  short getShortValue(String strFieldIdText, Model model) {
		// 解析strFieldIdText,判断出是否为FieldId
		StringBuilder strbFieldIdText = new StringBuilder();
		boolean bIsFieldId = handleFieldIdText(strFieldIdText, strbFieldIdText);

		if (bIsFieldId == false) {
			return Short.parseShort(strbFieldIdText.toString());
		} else {
			JObject obj=model.getJObject(strbFieldIdText.toString());
			if(obj==null){
				throw new RuntimeException("short value invalid error:"+strFieldIdText+"\n"+model);
			}
			return Short.valueOf(obj.getValue()+"");
		}
	}

	/**
	 * 根据FieldIdText得到值
	 * 
	 * @param strFieldIdText
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public static  int getIntegerValue(String strFieldIdText, Model model) {
		// 解析strFieldIdText,判断出是否为FieldId
		StringBuilder strbFieldIdText = new StringBuilder();
		boolean bIsFieldId = handleFieldIdText(strFieldIdText, strbFieldIdText);

		if (bIsFieldId == false) {
			return Integer.parseInt(strbFieldIdText.toString());
		} else {
			JObject obj=model.getJObject(strbFieldIdText.toString());
			if(obj==null){
				throw new RuntimeException("int value invalid error:"+strFieldIdText+"\n"+model);
			}
			return Integer.valueOf(obj.getValue()+"");
		}
	}

	/**
	 * 根据FieldIdText得到值
	 * 
	 * @param strFieldIdText
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public static  float getFloatValue(String strFieldIdText, Model model) {
		// 解析strFieldIdText,判断出是否为FieldId
		StringBuilder strbFieldIdText = new StringBuilder();
		boolean bIsFieldId = handleFieldIdText(strFieldIdText, strbFieldIdText);

		if (bIsFieldId == false) {
			return Float.parseFloat(strbFieldIdText.toString());
		} else {
			JObject obj=model.getJObject(strbFieldIdText.toString());
			if(obj==null){
				throw new RuntimeException("float value invalid error:"+strFieldIdText+"\n"+model);
			}
			return Float.valueOf(obj.getValue()+"");
		}
	}

	/**
	 * 根据FieldIdText得到值
	 * 
	 * @param strFieldIdText
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public static  double getDoubleValue(String strFieldIdText, Model model) {
		// 解析strFieldIdText,判断出是否为FieldId
		StringBuilder strbFieldIdText = new StringBuilder();
		boolean bIsFieldId = handleFieldIdText(strFieldIdText, strbFieldIdText);

		if (bIsFieldId == false) {
			return Double.parseDouble(strbFieldIdText.toString());
		} else {
			JObject obj=model.getJObject(strbFieldIdText.toString());
			if(obj==null){
				throw new RuntimeException("double value invalid error:"+strFieldIdText+"\n"+model);
			}
			return Double.valueOf(obj.getValue()+"");
		}
	}

	/**
	 * 根据FieldIdText得到值
	 * 
	 * @param strFieldIdText
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public static  Long getLongValue(String strFieldIdText, Model model) {
		// 解析strFieldIdText,判断出是否为FieldId
		StringBuilder strbFieldIdText = new StringBuilder();
		boolean bIsFieldId = handleFieldIdText(strFieldIdText, strbFieldIdText);

		if (bIsFieldId == false) {
			return Long.parseLong(strbFieldIdText.toString());
		} else {
			JObject obj=model.getJObject(strbFieldIdText.toString());
			if(obj==null){
				throw new RuntimeException("long value invalid error:"+strFieldIdText+"\n"+model);
			}
			return Long.valueOf(obj.getValue()+"");
		}
	}

	/**
	 * 根据FieldIdText得到值
	 * 
	 * @param strFieldIdText
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public static  String getStringValue(String strFieldIdText, Model model) {
		// 解析strFieldIdText,判断出是否为FieldId
		StringBuilder strbFieldIdText = new StringBuilder();
		boolean bIsFieldId = handleFieldIdText(strFieldIdText, strbFieldIdText);

		if (bIsFieldId == false) {
			return strbFieldIdText.toString();
		} else {
			JObject obj=model.getJObject(strbFieldIdText.toString());
			if(obj==null){
				return null;
			}
			return String.valueOf(obj.getValue());
		}
	}

	/**
	 * 根据FieldIdText得到值
	 * 
	 * @param strFieldIdText
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public static  String getDateValue(String strFieldIdText, Model model) {
		// 解析strFieldIdText,判断出是否为FieldId
		StringBuilder strbFieldIdText = new StringBuilder();
		boolean bIsFieldId = handleFieldIdText(strFieldIdText, strbFieldIdText);

		if (bIsFieldId == false) {
			return strbFieldIdText.toString();
		} else {
			JObject obj=model.getJObject(strbFieldIdText.toString());
			if(obj==null){
				return null;
			}
			return String.valueOf(obj.getValue());
		}
	}

	/**
	 * 根据FieldIdText得到值
	 * 
	 * @param strFieldIdText
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public static  String getTimeValue(String strFieldIdText, Model model) {
		// 解析strFieldIdText,判断出是否为FieldId
		StringBuilder strbFieldIdText = new StringBuilder();
		boolean bIsFieldId = handleFieldIdText(strFieldIdText, strbFieldIdText);

		if (bIsFieldId == false) {
			return strbFieldIdText.toString();
		} else {
			JObject obj=model.getJObject(strbFieldIdText.toString());
			if(obj==null){
				return null;
			}
			return String.valueOf(obj.getValue());
		}
	}

	/**
	 * 根据FieldIdText得到值
	 * 
	 * @param strFieldIdText
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public static  String getDateTimeValue(String strFieldIdText, Model model) {
		// 解析strFieldIdText,判断出是否为FieldId
		StringBuilder strbFieldIdText = new StringBuilder();
		boolean bIsFieldId = handleFieldIdText(strFieldIdText, strbFieldIdText);

		if (bIsFieldId == false) {
			return strbFieldIdText.toString();
		} else {
			JObject obj=model.getJObject(strbFieldIdText.toString());
			if(obj==null){
				return null;
			}
			return obj.getValue()+"";
		}
	}

	public static  Object getFieldValue(String strFieldId, Model model) {
		if (strFieldId.indexOf('{') >= 0) {
			strFieldId = getFieldId(strFieldId, model);
		}
		return model.getObject(strFieldId);
	}

	public static  String getFieldId(String strFieldIdText) {
		int nStartIndex = strFieldIdText.indexOf('{');
		if (nStartIndex < 0) {
			return strFieldIdText;
		}
		if (strFieldIdText.endsWith("}") == false) {
			throw new RuntimeException("Invalid field id: " + strFieldIdText);
		}

		return strFieldIdText.substring(1, strFieldIdText.length() - 1);
	}

	/**
	 * 根据FieldId text获得FieldId，允许嵌套
	 * 
	 * @param strFieldIdText
	 * @return
	 */
	public static  String getFieldId(String strFieldIdText, Model model) {
		int nStartIndex = strFieldIdText.indexOf('{');
		if (nStartIndex < 0) {
			return strFieldIdText;
		}

		// 存在{}
		int nEndIndex = findMatchedChar(nStartIndex, strFieldIdText);
		String strSubFieldId = strFieldIdText.substring(nStartIndex + 1, nEndIndex);
		String strSubFieldValue = getFieldValue(strSubFieldId, model).toString();

		return getFieldId(
				strFieldIdText.substring(0, nStartIndex) + strSubFieldValue + strFieldIdText.substring(nEndIndex + 1),
				model);
	}
	
	// 去掉{和}，并得到是否为FieldId
	public static  boolean handleFieldIdText(String strFieldIdText, StringBuilder strbOutput) {
		if (strFieldIdText == null || strFieldIdText.length() == 0) {
			return false;
		}
		strbOutput.setLength(0);

		char chFirst = strFieldIdText.charAt(0);
		int nIndex = 0;
		int nLength = strFieldIdText.length();
		while (true) {
			if (nIndex >= nLength) {
				break;
			}

			char ch = strFieldIdText.charAt(nIndex);
			if (ch == '{' || ch == '}') {
				if (nIndex == nLength - 1) {
					break;
				}
				if (strFieldIdText.charAt(nIndex + 1) == ch) {
					strbOutput.append(ch);
				}
			} else {
				strbOutput.append(ch);
			}
			nIndex++;
		}

		if (chFirst == '{' && strbOutput.charAt(0) != '{') {// 为FieldId
			return true;
		} else {// 为一般文本
			return false;
		}
	}
	
	/**
	 * 设置变量值
	 * @param sv
	 * @param model
	 */
	public static void setVar(SetVar sv, Model model) {
		String strVarId = sv.getVarId();
		String strFieldId = strVarId.substring(1, strVarId.length() - 1);
		switch (sv.getType().getType()) {
		case SetVarTypeType.BYTE_TYPE:
			model.set(strFieldId, Byte.parseByte(sv.getValue()));
			break;
		case SetVarTypeType.SHORT_TYPE:
			model.set(strFieldId, Short.parseShort(sv.getValue()));
			break;
		case SetVarTypeType.INTEGER_TYPE:
			model.set(strFieldId, Integer.parseInt(sv.getValue()));
			break;
		case SetVarTypeType.LONG_TYPE:
			model.set(strFieldId, Long.parseLong(sv.getValue()));
			break;
		case SetVarTypeType.FLOAT_TYPE:
			model.set(strFieldId, Float.parseFloat(sv.getValue()));
			break;
		case SetVarTypeType.DOUBLE_TYPE:
			model.set(strFieldId, Double.parseDouble(sv.getValue()));
			break;
		case SetVarTypeType.STRING_TYPE:
			model.set(strFieldId, sv.getValue());
			break;
		case SetVarTypeType.DATE_TYPE:
			model.set(strFieldId, sv.getValue());
			break;
		case SetVarTypeType.TIME_TYPE:
			model.set(strFieldId, sv.getValue());
			break;
		case SetVarTypeType.DATETIME_TYPE:
			model.set(strFieldId, sv.getValue());
			break;
		default:
			throw new RuntimeException("Invalid type " + sv.getType().toString());
		}
	}
	
	
	
	
	
	
	
	
	
	/**
	 * 查找到 nIndex 位置的字符的匹配字符
	 * 
	 * @param nIndex
	 * @param strText
	 * @return
	 */
	public static int findMatchedChar(int nIndex, String strText) {
		return findMatchedChar(nIndex, strText.toCharArray());
	}

	/**
	 * 查找到 nIndex 位置的字符的匹配字符
	 * 
	 * @param nIndex
	 * @param strText
	 * @return
	 */
	public static int findMatchedChar(int nIndex, String strText, char chSpec) {
		return findMatchedChar(nIndex, strText.toCharArray(), chSpec);
	}

	/**
	 * 查找到 nIndex 位置的字符的匹配字符
	 * 
	 * @param nIndex
	 * @param strText
	 * @return
	 */
	public static int findMatchedChar(int nIndex, char[] chsText) {
		if (nIndex < 0) {
			return -1;
		}

		char chChar = chsText[nIndex];
		int nCount = 0;
		int nStartIndex = -1;
		int nEndIndex = -1;

		char chFind = ' ';
		switch (chChar) {
		case '(':
			chFind = ')';
			break;
		case '{':
			chFind = '}';
			break;
		case '[':
			chFind = ']';
			break;
		case ')':
			chFind = '(';
			break;
		case '}':
			chFind = '{';
			break;
		case ']':
			chFind = '[';
			break;
		default:
			return -1;
		}

		int nLength = chsText.length;
		switch (chChar) {
		case '(':
		case '{':
		case '[':
			for (int i = nIndex + 1; i < nLength; i++) {
				char ch = chsText[i];

				if (ch == chChar) {
					nCount++;
				} else if (ch == chFind) {
					if (nCount == 0) {
						nEndIndex = i;
						break;
					} else {
						nCount--;
					}
				}
			}
			return nEndIndex;
		case ')':
		case '}':
		case ']':
			for (int i = nIndex - 1; i >= 0; i--) {
				char ch = chsText[i];

				if (ch == chChar) {
					nCount++;
				} else if (ch == chFind) {
					if (nCount == 0) {
						nStartIndex = i;
						break;
					} else {
						nCount--;
					}
				}
			}
			return nStartIndex;
		default:
			return -1;
		}
	}

	/**
	 * 查找到 nIndex 位置的字符的匹配字符
	 * 
	 * @param nIndex
	 * @param strText
	 * @return
	 */
	public static int findMatchedChar(int nIndex, char[] chsText, char chSpec) {
		if (nIndex < 0) {
			return -1;
		}

		char chChar = chsText[nIndex];
		int nCount = 0;
		int nStartIndex = -1;
		int nEndIndex = -1;

		char chFind = ' ';
		switch (chChar) {
		case '(':
			chFind = ')';
			break;
		case '{':
			chFind = '}';
			break;
		case '[':
			chFind = ']';
			break;
		case ')':
			chFind = '(';
			break;
		case '}':
			chFind = '{';
			break;
		case ']':
			chFind = '[';
			break;
		default:
			return -1;
		}

		int nLength = chsText.length;
		switch (chChar) {
		case '(':
		case '{':
		case '[':
			for (int i = nIndex + 1; i < nLength; i++) {
				char ch = chsText[i];

				if (ch == chChar && chsText[i - 1] != chSpec) {
					nCount++;
				} else if (ch == chFind && chsText[i - 1] != chSpec) {
					if (nCount == 0) {
						nEndIndex = i;
						break;
					} else {
						nCount--;
					}
				}
			}
			return nEndIndex;
		case ')':
		case '}':
		case ']':
			for (int i = nIndex - 1; i >= 0; i--) {
				char ch = chsText[i];

				if (i == 0 && ch == chChar || i > 0 && chsText[i - 1] != chSpec) {
					nCount++;
				} else if (i == 0 && ch == chFind || i > 0 && chsText[i - 1] != chSpec) {
					if (nCount == 0) {
						nStartIndex = i;
						break;
					} else {
						nCount--;
					}
				}
			}
			return nStartIndex;
		default:
			return -1;
		}
	}
	
	
	
	
	final public static String getFieldValue(Model model, Model modelResponse, String strFieldId) {
		String strValue = null;
		try {
			strValue = modelResponse.getString(strFieldId);
		} catch (Exception e) {
			try {
				strValue = model.getString(strFieldId);
			} catch (Exception e2) {
			}
		}

		return strValue;
	}

	public static String getModelText(Model model, Model modelResponse, String strSource) {
		StringBuilder strbLog = new StringBuilder();

		int nLength = strSource.length();
		StringBuilder strbParaName = new StringBuilder(nLength);
		int nState = 0;// 0 : {} 之外的字符, 1: {}之内字符.
		int i = 0;
		while (i < nLength) {
			char ch = strSource.charAt(i);
			switch (ch) {
			case '{':
				if (nState == 0) {// 在{}之外
					if (i + 1 < nLength && strSource.charAt(i + 1) == '{') {// 为普通字符
						strbLog.append("{");
						i += 2;
					} else {// 字段开始
						nState = 1;
						i++;
					}
				} else {// 在{}之内，语法错误
					return strSource;
				}
				break;
			case '}':
				if (nState == 0) {// 在{}之外
					if (i + 1 < nLength && strSource.charAt(i + 1) == '}') {// 为普通字符
						strbLog.append("}");
						i++;
					} else {// 语法错误
						return strSource;
					}
				} else {// 在{}之内，字段结束
					if (strbParaName.length() == 0) {// 语法错误
						return strSource;
					}
					nState = 0;

					String strFieldId = strbParaName.toString();
					String strFieldValue = getFieldValue(model, modelResponse, strFieldId);
					if (strFieldValue == null) {
						strFieldValue = '{' + strFieldId + '}';
					}
					strbLog.append(strFieldValue);
					strbParaName.setLength(0);
				}
				i++;
				break;
			default:
				if (nState == 0) {// 在{}之外
					strbLog.append(ch);
				} else {
					strbParaName.append(ch);
				}
				i++;
				break;
			}
		}

		if (nState == 1) {// 语法错误
			return strSource;
		}

		return strbLog.toString();
	}

	public static String getModelText(Model model, String strSource) {
		StringBuilder strbLog = new StringBuilder();

		int nLength = strSource.length();
		StringBuilder strbParaName = new StringBuilder(nLength);
		int nState = 0;// 0 : {} 之外的字符, 1: {}之内字符.
		int i = 0;
		while (i < nLength) {
			char ch = strSource.charAt(i);
			switch (ch) {
			case '{':
				if (nState == 0) {// 在{}之外
					if (i + 1 < nLength && strSource.charAt(i + 1) == '{') {// 为普通字符
						strbLog.append("{");
						i += 2;
					} else {// 字段开始
						nState = 1;
						i++;
					}
				} else {// 在{}之内，语法错误
					return strSource;
				}
				break;
			case '}':
				if (nState == 0) {// 在{}之外
					if (i + 1 < nLength && strSource.charAt(i + 1) == '}') {// 为普通字符
						strbLog.append("}");
						i++;
					} else {// 语法错误
						return strSource;
					}
				} else {// 在{}之内，字段结束
					if (strbParaName.length() == 0) {// 语法错误
						return strSource;
					}
					nState = 0;

					String strFieldId = strbParaName.toString();
					String strFieldValue = null;
					try {
						if (strFieldId.indexOf("m:") == 0) {// 使用数学表达式计算出来的值直接替换
							strFieldId = strFieldId.substring(2);
							long lValue = MathExt.getMathValue(strFieldId, model);
							strFieldValue = "" + lValue;
						} else {
							strFieldValue = model.getString(strFieldId);
						}
					} catch (Exception e) {
					}
					if (strFieldValue == null) {
						strFieldValue = '{' + strFieldId + '}';
					}
					strbLog.append(strFieldValue);
					strbParaName.setLength(0);
				}
				i++;
				break;
			default:
				if (nState == 0) {// 在{}之外
					strbLog.append(ch);
				} else {
					strbParaName.append(ch);
				}
				i++;
				break;
			}
		}

		if (nState == 1) {// 语法错误
			return strSource;
		}

		return strbLog.toString();
	}
	
	
	public static HashSet<String> getDecryptFieldNames(String strDecryptFieldNames) {
		String[] strsNames = Utility.splitString(strDecryptFieldNames, '|');
		HashSet<String> hsNames = new HashSet<String>();
		for (String strName : strsNames) {
			hsNames.add(strName.toLowerCase());
		}

		return hsNames;
	}
	
	public static JObject getObject(Model model, String strFieldId) {
		String strReplace = null;
		String[] strsItems = Utility.splitString(strFieldId, '|');
		JObject object = null;
		if (strsItems.length == 1) {// 没有取代值
			object = model.getJObject(strFieldId);
		} else {// 有取代值
			try {
				object = model.getJObject(strsItems[0]);
				if (object == null) {
					strReplace = ValueUtils.getStringValue(strsItems[1], model);
					object = new JObject(DataType._string, strsItems[1]);
				}
			} catch (Exception e) {
				if (strsItems[1].equalsIgnoreCase("null") == true) {
					return null;
				}
				strReplace = ValueUtils.getStringValue(strsItems[1], model);
				object = new JObject(DataType._string, strReplace);
			}
		}
		return object;
	}
	
	public static void handleReturn(com.gavin.schema.dataservice.Return returnObject, Model model,Model modelResponse, Result ret) throws SQLException {
		int nReturnItemCount = returnObject.getReturnItemCount();
		for (int j = 0; j < nReturnItemCount; j++) {
			String strFieldId = returnObject.getReturnItem(j).getFieldId();
			String strValueId = returnObject.getReturnItem(j).getValueId();
			strFieldId = strFieldId.substring(1, strFieldId.length() - 1);
			strValueId = strValueId.substring(1, strValueId.length() - 1);
			JObject object = null;
			try {
				object = model.getJObject(strValueId);
			} catch (Exception e) {
				continue;
			}

			// 输出
			if (object == null) {
				continue;
			}
			DataType nType = object.getType();
			Object objValue = object.getValue();
			switch (nType) {
			case _boolean: {
				modelResponse.set(strFieldId, ((Boolean) objValue).booleanValue());
				break;
			}
			case _byte: {
				modelResponse.set(strFieldId, ((Byte) objValue).byteValue());
				break;
			}
			case _short: {
				modelResponse.set(strFieldId, ((Short) objValue).shortValue());
				break;
			}
			case _int: {
				modelResponse.set(strFieldId, ((Integer) objValue).intValue());
				break;
			}
			case _long: {
				modelResponse.set(strFieldId, ((Long) objValue).longValue());
				break;
			}
			case _float: {
				modelResponse.set(strFieldId, ((Float) objValue).floatValue());
				break;
			}
			case _double: {
				modelResponse.set(strFieldId, ((Double) objValue).doubleValue());
				break;
			}
			case _string: {
				modelResponse.set(strFieldId, ((String) objValue));
				break;
			}
			case _date: {
				modelResponse.set(strFieldId, ((String) objValue));
				break;
			}
			case _time: {
				modelResponse.set(strFieldId, ((String) objValue));
				break;
			}
			case _datetime: {
				modelResponse.set(strFieldId, ((String) objValue));
				break;
			}
			case _m: {
				if(nReturnItemCount==1){//如果只有一个结果则拷贝返回 update by wdm 2017-12-23
					modelResponse.copy((Model) objValue);
				}else{
					modelResponse.set(strFieldId, (Model) objValue);
				}
				break;
			}
			case _booleans: {
				modelResponse.set(strFieldId, ((boolean[]) objValue));
				break;
			}
			case _bytes: {
				modelResponse.set(strFieldId, ((byte[]) objValue));
				break;
			}
			case _shorts: {
				modelResponse.set(strFieldId, ((short[]) objValue));
				break;
			}
			case _ints: {
				modelResponse.set(strFieldId, ((int[]) objValue));
				break;
			}
			case _longs: {
				modelResponse.set(strFieldId, ((long[]) objValue));
				break;
			}
			case _floats: {
				modelResponse.set(strFieldId, ((float[]) objValue));
				break;
			}
			case _doubles: {
				modelResponse.set(strFieldId, ((double[]) objValue));
				break;
			}
			case _strings: {
				modelResponse.set(strFieldId, ((String[]) objValue));
				break;
			}
			case _dates: {
				modelResponse.set(strFieldId, ((String[]) objValue));
				break;
			}
			case _times: {
				modelResponse.set(strFieldId, ((String[]) objValue));
				break;
			}
			case _datetimes: {
				modelResponse.set(strFieldId, ((String[]) objValue));
				break;
			}
			case _ms: {
				modelResponse.set(strFieldId, ((Model[]) objValue));
				break;
			}
			default: {
				throw new SQLException("Unsupported type " + nType);
			}
			}
		}

		int nReturnCode = ValueUtils.getIntegerValue(returnObject.getCode(), null);
		String strReturnText = ValueUtils.getModelText(model, returnObject.getText());
		String strReturnInfo = ValueUtils.getModelText(model, returnObject.getInfo());

		ret.setCode(nReturnCode);
		ret.setInfo(strReturnInfo);
		ret.setText(strReturnText);
	}
	
	
	public static Request newRequest(HandleTrans htItem, Model model, Model modelResponse) {
		Request request=Request.build(htItem.getServiceName(), htItem.getTransName());
		HandleTransItem[] ais = htItem.getHandleTransItem();
		for (int i = 0; i < ais.length; i++) {
			Field field = ais[i].getField();
			if (field != null) {
				String strValueId = Model.getFieldId(field.getValueId());
				JObject objExt = null;
				try {
					objExt = model.getJObject(strValueId);
				} catch (Exception e) {
				}
				if (objExt == null) {
					try {
						objExt = modelResponse.getJObject(strValueId);
					} catch (Exception e) {
						continue;
					}
				}
				request.model().setJObject(Model.getFieldId(field.getId()), objExt);
			} else {
				Value value = ais[i].getValue();
				switch (value.getType().getType()) {
				case ValueTypeType.BOOLEAN_TYPE: {
					request.model().set(Model.getFieldId(value.getId()),
							Boolean.parseBoolean(value.getValue()));
					break;
				}
				case ValueTypeType.BYTE_TYPE: {
					request.model().set(Model.getFieldId(value.getId()),
							Byte.parseByte(value.getValue()));
					break;
				}
				case ValueTypeType.SHORT_TYPE: {
					request.model().set(Model.getFieldId(value.getId()),
							Short.parseShort(value.getValue()));
					break;
				}
				case ValueTypeType.INTEGER_TYPE: {
					request.model().set(Model.getFieldId(value.getId()),
							Integer.parseInt(value.getValue()));
					break;
				}
				case ValueTypeType.LONG_TYPE: {
					request.model().set(Model.getFieldId(value.getId()),
							Long.parseLong(value.getValue()));
					break;
				}
				case ValueTypeType.FLOAT_TYPE: {
					request.model().set(Model.getFieldId(value.getId()),
							Float.parseFloat(value.getValue()));
					break;
				}
				case ValueTypeType.DOUBLE_TYPE: {
					request.model().set(Model.getFieldId(value.getId()),
							Double.parseDouble(value.getValue()));
					break;
				}
				case ValueTypeType.STRING_TYPE: {
					request.model().set(Model.getFieldId(value.getId()), value.getValue());
					break;
				}
				case ValueTypeType.DATE_TYPE: {
					request.model().set(Model.getFieldId(value.getId()), value.getValue());
					break;
				}
				case ValueTypeType.TIME_TYPE: {
					request.model().set(Model.getFieldId(value.getId()), value.getValue());
					break;
				}
				case ValueTypeType.DATETIME_TYPE: {
					request.model().set(Model.getFieldId(value.getId()), value.getValue());
					break;
				}
				}
			}
		}

		return request;
	}
	
	public static void setValueByDataType(Model model,String strOutputId,DataType nType,Object objValue) throws SQLException{
//		DataType nType = objFieldValue.getType();
//		Object objValue = objFieldValue.getValue();

//		String strOutputId = selectField.getOutputId();
//		strOutputId = strOutputId.substring(1, strOutputId.length() - 1);
		switch (nType) {
			case _boolean: {
				model.set(strOutputId, ((Boolean) objValue).booleanValue());
				break;
			}
			case _byte: {
				model.set(strOutputId, ((Byte) objValue).byteValue());
				break;
			}
			case _short: {
				model.set(strOutputId, ((Short) objValue).shortValue());
				break;
			}
			case _int: {
				model.set(strOutputId, ((Integer) objValue).intValue());
				break;
			}
			case _long: {
				model.set(strOutputId, ((Long) objValue).longValue());
				break;
			}
			case _float: {
				model.set(strOutputId, ((Float) objValue).floatValue());
				break;
			}
			case _double: {
				model.set(strOutputId, ((Double) objValue).doubleValue());
				break;
			}
			case _string: {
				model.set(strOutputId, ((String) objValue));
				break;
			}
			case _date: {
				model.set(strOutputId, ((String) objValue));
				break;
			}
			case _time: {
				model.set(strOutputId, ((String) objValue));
				break;
			}
			case _datetime: {
				model.set(strOutputId, ((String) objValue));
				break;
			}
			case _m: {
				model.set(strOutputId, ((Model) objValue));
				break;
			}
			case _booleans: {
				model.set(strOutputId, ((boolean[]) objValue));
				break;
			}
			case _bytes: {
				model.set(strOutputId, ((byte[]) objValue));
				break;
			}
			case _shorts: {
				model.set(strOutputId, ((short[]) objValue));
				break;
			}
			case _ints: {
				model.set(strOutputId, ((int[]) objValue));
				break;
			}
			case _longs: {
				model.set(strOutputId, ((long[]) objValue));
				break;
			}
			case _floats: {
				model.set(strOutputId, ((float[]) objValue));
				break;
			}
			case _doubles: {
				model.set(strOutputId, ((double[]) objValue));
				break;
			}
			case _strings: {
				model.set(strOutputId, ((String[]) objValue));
				break;
			}
			case _dates: {
				model.set(strOutputId, ((String[]) objValue));
				break;
			}
			case _times: {
				model.set(strOutputId, ((String[]) objValue));
				break;
			}
			case _datetimes: {
				model.set(strOutputId, ((String[]) objValue));
				break;
			}
			case _ms: {
				model.set(strOutputId, ((Model[]) objValue));
				break;
			}
			default: {
				throw new SQLException("Unsupported type " + nType);
			}
		}	
	}
}
